set(CPACK_PACKAGE_NAME webview)
set(CPACK_PACKAGE_VENDOR webview organization)
set(CPACK_PACKAGE_VERSION ${WEBVIEW_VERSION})
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "A tiny cross-platform webview library for C/C++ to build modern cross-platform GUIs")
set(CPACK_PACKAGE_INSTALL_DIRECTORY "${CPACK_PACKAGE_NAME}")
set(CPACK_RESOURCE_FILE_LICENSE ${PROJECT_SOURCE_DIR}/LICENSE)
set(CPACK_VERBATIM_VARIABLES TRUE)

string(TOLOWER "${CMAKE_GENERATOR_PLATFORM}" CMAKE_GENERATOR_PLATFORM_LC)
if(MSVC)
    if(CMAKE_GENERATOR_PLATFORM_LC STREQUAL "arm64")
        set(CMAKE_SYSTEM_PROCESSOR arm64)
    elseif(CMAKE_GENERATOR_PLATFORM_LC STREQUAL "amd64" OR CMAKE_GENERATOR_PLATFORM_LC STREQUAL "x64")
        set(CMAKE_SYSTEM_PROCESSOR x86_64)
    elseif(CMAKE_GENERATOR_PLATFORM_LC STREQUAL "win32" OR CMAKE_GENERATOR_PLATFORM_LC STREQUAL "x86")
        set(CMAKE_SYSTEM_PROCESSOR x86)
    endif()
endif()

string(TOLOWER "${CMAKE_SYSTEM_NAME}" CMAKE_SYSTEM_NAME_LC)
string(TOLOWER "${CMAKE_SYSTEM_PROCESSOR}" CMAKE_SYSTEM_PROCESSOR_LC)
string(TOLOWER "${CMAKE_CXX_COMPILER_ID}" CMAKE_CXX_COMPILER_ID_LC)

if(MSVC AND MSVC_TOOLSET_VERSION)
    set(COMPILER_VERSION "${MSVC_TOOLSET_VERSION}")
else()
    string(REPLACE "." ";" COMPILER_VERSION_LIST "${CMAKE_CXX_COMPILER_VERSION}")
    list(GET COMPILER_VERSION_LIST 0 COMPILER_VERSION_MAJOR)
    list(GET COMPILER_VERSION_LIST 1 COMPILER_VERSION_MINOR)
    set(COMPILER_VERSION "${COMPILER_VERSION_MAJOR}.${COMPILER_VERSION_MINOR}")
endif()
set(COMPILER "${CMAKE_CXX_COMPILER_ID_LC}_${COMPILER_VERSION}")

if(WEBVIEW_USE_STATIC_MSVC_RUNTIME)
    set(RUNTIME_SUFFIX "-static_rt")
endif()

if(APPLE AND CMAKE_OSX_ARCHITECTURES MATCHES ";")
    set(PACKAGE_NAME_ARCH universal)
else()
    set(PACKAGE_NAME_ARCH ${CMAKE_SYSTEM_PROCESSOR_LC})
endif()

if((MSYS OR MINGW) AND DEFINED ENV{MSYSTEM})
    string(TOLOWER "$ENV{MSYSTEM}" MSYSTEM)
    set(MSYSTEM_SUFFIX "-${MSYSTEM}")
endif()

file(TO_CMAKE_PATH "${PROJECT_BINARY_DIR}" IGNORED_BUILD_DIR)
string(REPLACE "\\" "\\\\" IGNORED_BUILD_DIR "${IGNORED_BUILD_DIR}")
set(CPACK_SOURCE_IGNORE_FILES
    \\.git/
    "^${IGNORED_BUILD_DIR}"
)
set(CPACK_SOURCE_GENERATOR TGZ)

# File names
set(WEBVIEW_ARCHIVE_SYSTEM_ARCH_FILE_NAME "${CPACK_PACKAGE_NAME}_v${CPACK_PACKAGE_VERSION}-${COMPILER}${MSYSTEM_SUFFIX}${RUNTIME_SUFFIX}-${CMAKE_SYSTEM_NAME_LC}-${PACKAGE_NAME_ARCH}")
set(WEBVIEW_ARCHIVE_UNIVERSAL_FILE_NAME "${CPACK_PACKAGE_NAME}_v${CPACK_PACKAGE_VERSION}")
set(CPACK_SOURCE_PACKAGE_FILE_NAME "${WEBVIEW_ARCHIVE_UNIVERSAL_FILE_NAME}-source")

# Set up custom packaging script for the "External" CPack generator
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/external_package.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/external_package.cmake" @ONLY)
set(CPACK_EXTERNAL_PACKAGE_SCRIPT "${CMAKE_CURRENT_BINARY_DIR}/external_package.cmake")
set(CPACK_EXTERNAL_ENABLE_STAGING TRUE)

include(CPack)
